/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;


import engine.Singleton;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import sql.CreateTables;
import webClient.HttpClient;

/**
 *
 * @author Alexx
 */
public class MultiThreadedServer extends Thread
{
  public static int port;
  
  public MultiThreadedServer()
  {
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src\\SpringXMLConfig.xml"));
    server.Port ports = (server.Port) factory.getBean("port"); 
    port = ports.getPort();
  }
    

     
    
    public static void main(String[] args) throws IOException, SQLException, Throwable
    {
      BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src\\SpringXMLConfig.xml"));
      server.Port ports = (server.Port) factory.getBean("port");
      port = ports.getPort();
    
        ServerSocket s = new ServerSocket(port);
        System.out.println("Server Started port " + port);
        Singleton.getInstance();
        try {
       // CreateTables.creator();
        } catch (Exception e){}
        HttpClient hc = new HttpClient();
        hc.start();
        try
        {
            while (true)
            {
         //     HttpClient.fire();
                Socket socket = s.accept();
                try
                {
                    ServeOneThread clients = new ServeOneThread(socket);
                    System.out.println("");
                    
                }
                catch (IOException e)
                {
                    socket.close();
                }
            }
        }
        finally
        {
            s.close();
        }
    }
    
    public void run()
    {
        HttpClient hc = new HttpClient();
        hc.start();
        try
        {
      ServerSocket s = new ServerSocket(port);
        System.out.println("Server Started port " + port);
          Singleton.getInstance();
          try {
       //     CreateTables.creator();
          } catch (Exception e) {
          }
     //   HttpClient.fire();
        try
        {
            while (true)
            {
         //     HttpClient.fire();
                Socket socket = s.accept();
                try
                {
                    ServeOneThread clients = new ServeOneThread(socket);
                     
                }
                catch (IOException e)
                {
                    socket.close();
                }
            }
        }
        finally
        {
            s.close();
        }
    }
    catch (IOException ex) {
      Logger.getLogger(MultiThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
    
    }
    
  }
}
